<?php 
	include_once "functions.php";
	function getMailBar($send_mails, $inbox_mails){

		$count_new = 0;

			
		DebugOut($inbox_mails,1,1);
		if(!is_array($inbox_mails)){
			$inbox_mails = array();
		}

		if(!is_array($send_mails)){
			$send_mails = array();
		}

		foreach ($inbox_mails as  $value) {
			$count_new += $value["mail_new"];
		}

		DebugOut("count_new", 0 , 0);
		DebugOut($count_new, 1 , 0);

?>

	<style type="text/css">
		.mail_list_inbox {
			display: none;
			border:1px solid grey;
		}
		.letter_body_inbox{
			display: none;
			border:1px solid grey;	
		}
		.new_letter {
			font-weight: bold;
			font-style: italic;
		}

	</style>

	<div class="mail_div">
		<div class="mail_bar">
			<span> Письма:  </span>
			<span class="Inbox"> <a href="#">  Входящие <span class="InboxNew"> <?php  echo $count_new; ?> </span> </a> </span>
			<span class="Send"> <a href="#"> Отправленные  </a> </span>
		</div>	

		<div class="send_mail">
			<form id="form_send_mail">
				<input type="text" name="user_id"> <br>
				<input type="text" name="mail_title">	<br>
				<textarea name="mail_body" id="" cols="30" rows="10"></textarea>
				<input type="submit" name="submit">
			</form>			
		</div>

		<div class="mail_list_send">
			
		</div>
		<div class="mail_list_inbox">
			<ul>
				<?php  
					foreach ($inbox_mails as  $mail) {
				?>
					<li>
						<a href="#" class="mail_title mail_id<?php echo $mail['mail_id']; ?>  
							<?php 
								if($mail["mail_new"] == 1){
									echo "new_letter";
								}
							?>	
						  "> <?php echo $mail["mail_title"] ?> </a>
						<div class="letter_body_inbox mail_body_id<?php echo $mail['mail_id'];?> "></div>
					</li>
				<?php
					}					
				?>
			</ul>
		</div>
	</div>
	<script>
		$(function(){
			$(".Inbox").click(function(){
				$(".mail_list_inbox").toggle();
			});

			$("#form_send_mail").submit(function(){
				var dataS = {
					mail_data:{
						method:"sendMail"
					}
				};
				
				dataS.mail_data['user_id']  = $("#form_send_mail [name=user_id]").val();
				dataS.mail_data['mail_title']  = $("#form_send_mail [name=mail_title]").val();
				dataS.mail_data['mail_body']  = $("#form_send_mail [name=mail_body]").val();
				
				console.log(dataS);


				$.ajax({
							type:'POST',
							url:ajaxurl + "?action=mail_ajax",
							data:dataS	
							})
							.done(function(resp){
								console.log(resp);
							});	

				return false;
			});

			$(".mail_title").click(function(){
				var classList =$(this).attr('class').split(/\s+/);
				var mail_id = 0;
				var mail_new = false;
				$.each(classList, function(idx, item){
					if(item.indexOf('mail_id') + 1) {
						console.log(item);
						mail_id = item.slice(item.length-1);
					}
					if(item.indexOf('mail_new') + 1) {
						mail_new = true;	
					}
				});
				var self_mail = this;




				if(mail_id > 0){
						var dataS = {
							mail_data:{
								method:"getMailInbox",
								mail_id:mail_id
							}
						};
					

						var div_id = ".mail_body_id"+mail_id
						if( $(div_id).is(':visible') ){
							$(div_id).toggle();
						}else{
							$.ajax({
							type:'POST',
							url:ajaxurl + "?action=mail_ajax",
							data:dataS	
							})
							.done(function(resp){
								//console.log(resp);
								resp = resp.slice(0,-1);
								//console.log(resp);
								
								$(div_id).text(resp);
								$(div_id).toggle();
								var dataS = {
									mail_data:{
										method:"setReaded",
										mail_id:mail_id
									}
								};	

								if(mail_new){
									setTimeout(function(){
										$.ajax({
										type:'POST',
										url:ajaxurl + "?action=mail_ajax",
										data:dataS	
										}).done(function(resp1){
											var $val = $(".InboxNew").text();
											if($val > 0){
												$val -= 1;
												$(".InboxNew").text($val);
											}
										});		
									},2000);
								}

							});	
						}
 
						
				
				}

			});			

		});

	</script>

<?php 
	}
?>
